package com.dudu.dataImport;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

public class SSoTest {
    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAITfy6V4fDvGrb";
    private static String accessKeySecret = "HNAj2vm1CN9qwUjMeXWeby0H2sk8Kc";

    public static void main(String[] args) {
	String srcBucketName = "yonyou-test-01";// yonyou-test-01
	String destBucketName = "yonyou-dudu-public";// yonyou-dudu-public
	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	ListObjectsRequest listObjectsRequest = new ListObjectsRequest(srcBucketName);
	// "/" 为文件夹的分隔符
	listObjectsRequest.setDelimiter("/");
	// 列出fun目录下的所有文件和文件夹
	listObjectsRequest.setPrefix("gaotest/201708/");//duduopen/201708
	listObjectsRequest.setMaxKeys(1000);
	try {
	    ObjectListing listing = ossClient.listObjects(listObjectsRequest);
	    // 遍历所有Object
	    System.out.println("Objects:" + listing.getObjectSummaries().size());
	    for (int i = 0; i < listing.getObjectSummaries().size(); i++) {
		OSSObjectSummary objectSummary = listing.getObjectSummaries().get(i);
		String key = objectSummary.getKey();
		// Object是否存在
		// boolean found = ossClient.doesObjectExist("<bucketName>",
		// "<key>")
		// 删除Object
		// ossClient.deleteObject("<bucketName>", "<key>");
		CopyObjectResult result = ossClient.copyObject(srcBucketName, key, destBucketName, key);
		System.out.println("序列号:" + (i+1) + ";文件名:" + key + "ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    ossClient.shutdown();
	}
    }

}
